import { ResponseResult } from "@/entity/ResponseResult";
import { UmsAddress } from "@/entity/UmsAddress";
import { AxiosError, AxiosResponse } from "axios";
import httpClient from "./http-client";
import store, { TOKEN_USER_ID } from "@/store";
import { db_addresses } from "@/database/address";

export class AddressService {
    baseUrl = "/api/v1.0/portal/address";

    public async list() {
        const userId = Number(store.getters[TOKEN_USER_ID]);
        return Promise.resolve(db_addresses.filter(it => it.memberId == userId));
        return httpClient.get(`${this.baseUrl}`).then((res: AxiosResponse<ResponseResult<UmsAddress[]>>) => {
            if (res.data.data) {
                const list = res.data.data;
                return Promise.resolve(list);
            } else {
                throw "no data";
            }
        });
    }

    public async retrieve(id: number) {
        const addr = db_addresses.find(it => it.id == id);
        if (addr == null) {
            throw new AxiosError();
        }
        return Promise.resolve(addr);
        return httpClient.get(`${this.baseUrl}/${id}`).then((res: AxiosResponse<ResponseResult<UmsAddress>>) => {
            if (res.data.data) {
                const addr = res.data.data;
                return Promise.resolve(addr);
            } else {
                throw "no data";
            }
        });
    }

    public async retrieveDefault() {
        const userId = Number(store.getters[TOKEN_USER_ID]);
        return Promise.resolve(db_addresses.filter(it => it.memberId == userId).find((it, idx) => idx == 0));
        return httpClient.get(`${this.baseUrl}/default`).then((res: AxiosResponse<ResponseResult<UmsAddress>>) => {
            return Promise.resolve(res.data.data);
        });
    }

    public async create(param: UmsAddress) {
        const userId = Number(store.getters[TOKEN_USER_ID]);

        const addr: UmsAddress = {
            ...param
        };
        addr.id = Math.max(...db_addresses.map(it => it.id)) + 1;
        addr.memberId = userId;

        db_addresses.push(addr);

        return Promise.resolve(addr.id);

        const body = {
            ...param
        };
        return httpClient.post(`${this.baseUrl}`, body).then((res: AxiosResponse<ResponseResult<number>>) => {
            if (res.data.data) {
                const id = res.data.data;
                return Promise.resolve(id);
            } else {
                throw "no data";
            }
        });
    }

    public async update(param: UmsAddress) {
        const userId = Number(store.getters[TOKEN_USER_ID]);

        const addr = db_addresses.find(it => it.id == param.id);
        if (addr == null) {
            throw new AxiosError("未找到数据");
        }
        addr.createdTime = param.createdTime;
        addr.id = param.id;
        addr.lastModifiedTime = param.lastModifiedTime;
        addr.memberId = param.memberId;
        addr.receiverAddress = param.receiverAddress;
        addr.receiverCity = param.receiverCity;
        addr.receiverCityCode = param.receiverCityCode;
        addr.receiverDistrict = param.receiverDistrict;
        addr.receiverDistrictCode = param.receiverDistrictCode;
        addr.receiverName = param.receiverName;
        addr.receiverPhone = param.receiverPhone;
        addr.receiverProvince = param.receiverProvince;
        addr.receiverProvinceCode = param.receiverProvinceCode;


        return Promise.resolve(addr.id);

        const body = {
            ...param
        };
        return httpClient.put(`${this.baseUrl}`, body).then((res: AxiosResponse<ResponseResult<number>>) => {
            if (res.data.data) {
                const id = res.data.data;
                return Promise.resolve(id);
            } else {
                throw "no data";
            }
        });
    }

    public async remove(ids: number[]) {
        const tmp = db_addresses.filter(it => !ids.includes(it.id));
        db_addresses.length = 0;
        db_addresses.push(...tmp);

        return Promise.resolve({ "code": 200, "message": "操作成功", "data": null });

        return httpClient.delete(`${this.baseUrl}?ids=${ids ? ids.join(",") : ""}`).then((res: AxiosResponse<ResponseResult<UmsAddress>>) => {
            if (res.data.data) {
                const addr = res.data.data;
                return Promise.resolve(addr);
            } else {
                throw "no data";
            }
        });
    }
}

const instance = new AddressService();
export default instance;